/**
 * 
 */
package cn.mxz.robots.province;

import java.util.Scanner;

import net.sf.json.JSONObject;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import cn.mxz.crawler_single.basic.Request;
import cn.mxz.rds.pojo.GaoKaoProvince;
import cn.mxz.rds.pojo.GaoKaoRank;
import cn.mxz.rds.service.GaoKaoProvinceService;
import cn.mxz.rds.service.GaoKaoRankService;
import cn.mxz.robots.basic.local.Exam;
import cn.mxz.robots.basic.local.IRank;
import cn.mxz.robots.pojo.Student;

/**
 * 湖北省
 * 排名: 完成
 * 去年排名:完成
 * 
 * @author cuizhennan<cuizhennan@mouge.wang>
 * @since 2015-06-22
 */
public class Hubei extends Exam implements IRank
{
	public static void main(String[] args)
	{
		Student student = new Student("132850314", "622826199305271916", "422828199710040025", "15622826152803", "bl");
		Hubei gansu = new Hubei(student);
		System.out.println(gansu.getCaptcha());
		System.out.println("请输入验证码：");
		@SuppressWarnings("resource")
		String captcha = new Scanner(System.in).next();
		System.out.println(gansu.login(captcha));
	}

	public Hubei(Student student)
	{
		super(student);
	}

	@Override
	public String getCaptcha()
	{
		Request captchaRequest = new Request("http://119.97.236.15/n_score/Kaptcha.do", mSha1);
		// Referer http://119.97.236.15/n_score/
		return curlFile(captchaRequest);
	}

	@Override
	public String login(String captcha)
	{
		Request loginRequest = new Request("http://119.97.236.15/n_score/QueryScore.do", mSha1);
		loginRequest.setCharSet("utf-8");
		loginRequest.addHeader("Referer", "http://119.97.236.15/n_score/");
		loginRequest.addParams("ticketNumber", mStudent.getStudentId());
		loginRequest.addParams("idNumber", mStudent.getIdCard());
		loginRequest.addParams("randCode", captcha);
		loginRequest.addParams("submit_button", "");
		String loginHtml = curlPost(loginRequest);
		JSONObject resultObject = new JSONObject();

		if (loginHtml.contains("查询结果"))
		{
			resultObject.put("result", LOGIN_SUCCESS);
			resultObject.put("data", loginParseHtml(loginHtml));
		}
		else if (loginHtml.contains("请确认输入信息"))
		{
			resultObject.put("result", ERROR);
			resultObject.put("data", NOT_EXIST_USER);
		}
		else if (loginHtml.contains("验证码输入错误"))
		{
			resultObject.put("result", ERROR);
			resultObject.put("data", WRONG_CAPTCHA);
		}
		else
		{
			resultObject.put("result", ERROR);
			resultObject.put("data", INTERNAL_SERVER_ERROR);
		}

		return resultObject.toString();
	}

	@Override
	public String loginParseHtml(String parseHtml)
	{
		Document loginDocument = Jsoup.parse(parseHtml);
		JSONObject result = new JSONObject();
		Elements trsif = loginDocument.select("table.border-table").get(0).select("tr");
		result.put("exam_id", mStudent.getStudentId());
		result.put("name", trsif.get(0).select("td").get(1).text());
		result.put("ranking", getRank(44, trsif.get(4).select("td").get(1).text(), mStudent.getType()));
		result.put("province", "湖北省");
		Elements trssc = loginDocument.select("table.border-table").get(1).select("tr");
		JSONObject scoreObject = new JSONObject();
		scoreObject.put("语文", trssc.get(1).select("td").get(1).text());
		scoreObject.put("数学", trssc.get(2).select("td").get(1).text());
		scoreObject.put("综合", trssc.get(4).select("td").get(1).text());
		scoreObject.put("外语", trssc.get(3).select("td").get(1).text());
		scoreObject.put("总分", trsif.get(4).select("td").get(1).text());
		result.put("score", scoreObject.toString());
		return result.toString();
	}

	@Override
	public void initRank()
	{
		// 文 http://www.hbee.edu.cn/html/gk/gz/2015-06/125889.html
		// 理 http://www.hbee.edu.cn/html/gk/gz/2015-06/125888.html
		Request request = new Request("http://www.hbee.edu.cn/html/gk/gz/2015-06/125888.html", mSha1);
		Document result = Jsoup.parse(curlGet(request));

		Elements trs = result.select("table").get(0).select("tr");
		for (int i = 2; i < trs.size(); i++)
		{
			String score = trs.get(i).select("td").get(0).html().replaceAll(" ", "");
			if (score.contains("&nbsp;&nbsp;&nbsp;"))
			{
				score = regexMatch(score, "&nbsp;&nbsp;&nbsp;(.*)");
			}
			else if (score.contains("&nbsp;&nbsp;"))
			{
				score = regexMatch(score, "&nbsp;&nbsp;(.*)");
			}
			else
			{
				score = regexMatch(score, ";(.*)");
			}

			String counter = trs.get(i).select("td").get(1).html().replaceAll(" ", "");
			if (counter.contains("&nbsp;&nbsp;&nbsp;"))
			{
				counter = regexMatch(counter, "&nbsp;&nbsp;&nbsp;(.*)");
			}
			else if (counter.contains("&nbsp;&nbsp;"))
			{
				counter = regexMatch(counter, "&nbsp;&nbsp;(.*)");
			}
			else
			{
				counter = regexMatch(counter, ";(.*)");
			}

			String ranking = trs.get(i).select("td").get(2).html().replaceAll(" ", "");
			if (ranking.contains("&nbsp;&nbsp;&nbsp;"))
			{
				ranking = regexMatch(ranking, "&nbsp;&nbsp;&nbsp;(.*)");
			}
			else if (ranking.contains("&nbsp;&nbsp;"))
			{
				ranking = regexMatch(ranking, "&nbsp;&nbsp;(.*)");
			}
			else if (ranking.contains("&nbsp;"))
			{
				ranking = regexMatch(ranking, "&nbsp;(.*)");
			}
			else
			{
				ranking = trs.get(i).select("td").get(2).text();
			}

			Integer scores = Integer.valueOf(score);
			Integer counters = Integer.valueOf(counter);
			Integer rankings = Integer.valueOf(ranking);

			System.out.println(scores + ":" + counters + ":" + rankings);

			GaoKaoProvinceService provinceService = new GaoKaoProvinceService();
			GaoKaoProvince province = provinceService.get(44);
			GaoKaoRankService rankService = new GaoKaoRankService();
			GaoKaoRank rank = new GaoKaoRank();
			rank.setScore(scores);
			rank.setCount(counters);
			rank.setType(LI_KE);
			rank.setRanking(rankings);
			rank.setGaoKaoProvince(province);
			rankService.save(rank);
		}
	}

	@Override
	public void lastYearRank()
	{
		//理 http://www.gxeduw.com/news/2014/107079.html | http://www.gxeduw.com/news/2014/107079_2.html
		//文 http://www.gxeduw.com/news/2014/107078.html | http://www.gxeduw.com/news/2014/107078_2.html
		Request request = new Request("http://www.gxeduw.com/news/2014/107078_2.html", mSha1);
		Document result = Jsoup.parse(curlGet(request));
		
		Elements trs = result.select("table").get(0).select("tr");
		for (int i = 2; i < trs.size(); i++)
		{
			Integer scores = Integer.parseInt(trs.get(i).select("td").get(0).text().replaceAll(" ", ""));
			Integer counters = Integer.parseInt(trs.get(i).select("td").get(1).text());
			Integer rankings = Integer.parseInt(trs.get(i).select("td").get(2).text());		
			System.out.println(scores + ":" + counters + ":" + rankings);
			
			GaoKaoProvinceService provinceService = new GaoKaoProvinceService();
			GaoKaoProvince province = provinceService.get(13);
			GaoKaoRankService rankService = new GaoKaoRankService();
			GaoKaoRank rank = new GaoKaoRank();
			rank.setScore(scores);
			rank.setCount(counters);
			rank.setType(WEN_KE);
			rank.setRanking(rankings);
			rank.setGaoKaoProvince(province);
			rankService.save(rank);
		}

	}
}
